#include <map>
#include <vector>
#include "../include/userstate.h"
#include "../include/card.h"

using namespace std;

namespace mtga {

	UserState::UserState(Deck* deck) {
		this->deck = deck;
		currentTurn = 0;
		//library = new vector<Card*>();
	}

	UserState::~UserState() {
	}

	void UserState::init() {
		std::cout << "Starting " << this->deck->getName() << " UserState" << std::endl;
		hand.clear();
		board.clear();
		library.clear();
		cementery.clear();
		map<Card*, int> cardMap = this->deck->getCards();
		typedef map<Card*, int>::iterator it_type;
		for (it_type iterator = cardMap.begin(); iterator != cardMap.end(); iterator++) {
			Card* card = iterator->first;
			int count = iterator->second;
			for (int i = 0; i < count; i++) {
				library.push_back(card);
			}
		}

	}

	void UserState::suffleLibrary() {
		int size = library.size();
		int entropy = 10 * size;
		for (int count = 0; count < entropy; count++) {
			int x = rand() % size;
			int y = rand() % size;
			if (x != y) {
				Card* tmp = library[x];
				library[x] = library[y];
				library[y] = tmp;
			}
		}
	}

	void UserState::drawCard(unsigned count) {
		for (unsigned i = 0; i < count; i++) {
			this->hand.push_back(library[0]);
			this->library.erase(library.begin());
		}
	}
}
